<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>table练习</title>
  <style>
    table{
      border-collapse: collapse;
    }
    td{
      border: 1px solid #000;
      padding: 8px 12px;
    }
  </style>
</head>
<body>
  <table>
    <tr>
      <td>1:1</td>
      <td>2:1</td>
      <td>3:1</td>
      <td>4:1</td>
    </tr>
    </tr>
      <td>1:2</td>
      <td>2:2</td>
      <td>3:2</td>
      <td>4:2</td>
    </tr>
    </tr>
      <td>1:3</td>
      <td>2:3</td>
      <td>3:3</td>
      <td>4:3</td>
    </tr>
    </tr>
      <td>1:4</td>
      <td>2:4</td>
      <td>3:4</td>
      <td>4:4</td>
    </tr>
  </table>

  <script>
    var body_el = document.body;
    var table_el = body_el.firstElementChild;
    // console.log(table_el);
    var tr_el = table_el.children[0];
    // console.log(table_el.rows);
    var tr_rows = table_el.rows;
    for(var tr_i=0; tr_i < tr_rows.length; tr_i++){
      // console.log(tr_rows[tr_i].cells[0])
      // console.log(tr_rows[0]);
      // console.log("tr_i",tr_i)
      // console.log("tr_rows[tr_i]", tr_rows[tr_i].cells.length)
      for(var td_j = 0; td_j < tr_rows[tr_i].cells.length; td_j++){
        if(tr_i === td_j){
          // console.log(tr_rows[tr_i].cells[td_j].innerText);
          var td_el = tr_rows[tr_i].cells[td_j];
          // 设置样式
          console.log(td_el)
          td_el.style.backgroundColor = "red";
          td_el.style.color = "white";
        }
        
      }
    }
  </script>
  
</body>
</html>